/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.service;

import java.util.List;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;
import com.boxin.ims.modules.wechat.entity.BusinessNewsReply;
import com.boxin.ims.modules.wechat.dao.BusinessNewsReplyDao;

/**
 * 业务消息Service
 * @author Jakemanse
 * @version 2014-01-13
 */
@Component
@Transactional(readOnly = true)
public class BusinessNewsReplyService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(BusinessNewsReplyService.class);
	
	@Autowired
	private BusinessNewsReplyDao businessNewsReplyDao;
	
	public BusinessNewsReply get(Long id) {
		return businessNewsReplyDao.findOne(id);
	}
	
	
	
	public List<BusinessNewsReply> findByTypeSourceId(String type,Long userId,String sourceId){
		DetachedCriteria dc = businessNewsReplyDao.createDetachedCriteria();
		
		
		if(com.thinkgem.jeesite.common.utils.StringUtils.isNotEmpty(sourceId)){
			dc.add(Restrictions.eq("sourceId", sourceId));
		}
		
		dc.add(Restrictions.eq("user.id", userId));
		dc.add(Restrictions.eq("type", type));
		dc.addOrder(Order.desc("sort"));
		return businessNewsReplyDao.find( dc);
	}
	
	
	/**
	 * @author Jakemanse
	 * @time 2014-3-31  上午11:17:15
	 * @function <p>个人业务图文回复  </p>
	 * @param userId
	 * @param sourceId
	 * @return
	 */
	public List<BusinessNewsReply> findBusinessNewsByTypeSourceId(Long userId,String sourceId){
		DetachedCriteria dc = businessNewsReplyDao.createDetachedCriteria();
		
		
		if(com.thinkgem.jeesite.common.utils.StringUtils.isNotEmpty(sourceId)){
			dc.add(Restrictions.eq("sourceId", sourceId));
		}
		
		dc.add(Restrictions.eq("user.id", userId));
		dc.add(Restrictions.eq("type", "business"));
		dc.addOrder(Order.desc("sort"));
		return businessNewsReplyDao.find( dc);
	}
	
	
	
	
	
	public Page<BusinessNewsReply> find(Page<BusinessNewsReply> page, BusinessNewsReply businessNewsReply) {
		DetachedCriteria dc = businessNewsReplyDao.createDetachedCriteria();
		if (businessNewsReply.getUser()!=null && businessNewsReply.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", businessNewsReply.getUser().getId()));
		}
		
		
		if (StringUtils.isNotEmpty(businessNewsReply.getTitle())){
			dc.add(Restrictions.like("title", "%"+businessNewsReply.getTitle()+"%"));
		}
		
		if (StringUtils.isNotEmpty(businessNewsReply.getType())){
			dc.add(Restrictions.like("type", "%"+businessNewsReply.getType()+"%"));
		}
		
		
		dc.addOrder(Order.desc("sort"));
		return businessNewsReplyDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(BusinessNewsReply businessNewsReply) {
		businessNewsReply.setUrl(StringEscapeUtils.unescapeHtml4(businessNewsReply.getUrl()));
		businessNewsReplyDao.save(businessNewsReply);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		businessNewsReplyDao.deleteById(id);
	}
	
}
